#ifndef _BLMC_H_
#define _BLMC_H_
#include <c8051f330.h>
#include "define.h"  

sbit	A_H_PIN	 =		P1^3;
sbit 	A_L_PIN	 =		P1^2;

sbit 	B_H_PIN	 =		P1^4;
sbit 	B_L_PIN	 =		P1^1;

sbit 	C_H_PIN	 =		P1^5;
sbit 	C_L_PIN	 =		P1^0;

sbit 	NULLA_PIN =		P0^5;
sbit 	NULLB_PIN =		P0^3;
sbit 	NULLC_PIN =		P0^1;
sbit 	MIDDLE_PIN =	P0^0;



#define A_H_ON		B_H_PIN = 0;C_H_PIN = 0;A_H_PIN = 1
#define B_H_ON		A_H_PIN = 0;C_H_PIN = 0;B_H_PIN = 1
#define C_H_ON		A_H_PIN = 0;B_H_PIN = 0;C_H_PIN = 1

#define A_H_OFF		A_H_PIN = 0
#define B_H_OFF		B_H_PIN = 0
#define C_H_OFF		C_H_PIN = 0

#define PWM_A_ON		A_H_ON
#define PWM_B_ON		B_H_ON
#define PWM_C_ON		C_H_ON

#define PWM_ON			PCA0CPM0=0x42
#define PWM_OFF			PCA0CPM0=0x00

#define PWM_NA_ON		P1&=0xF8;P1SKIP = 0x7B;XBR1=0x41
#define PWM_NB_ON		P1&=0xF8;P1SKIP = 0x7D;XBR1=0x41
#define PWM_NC_ON		P1&=0xF8;P1SKIP = 0x7E;XBR1=0x41


#define FET_OFF		XBR1=0x40;\
					P1&=0xC0
					//PWM_OFF

#define A_L_OFF		FET_OFF
#define B_L_OFF		FET_OFF
#define C_L_OFF		FET_OFF

#define A_L_ON		PWM_NA_ON 	
#define B_L_ON		PWM_NB_ON	
#define C_L_ON		PWM_NC_ON	


#define ALL_L_OFF	FET_OFF//A_L_OFF;B_L_OFF;C_L_OFF

#define MUTEX_NEG_A_ON	PWM_NA_ON//{ B_L_OFF;C_L_OFF;A_L_ON; }	
#define MUTEX_NEG_B_ON	PWM_NB_ON//{ A_L_OFF;C_L_OFF;B_L_ON; }	
#define MUTEX_NEG_C_ON	PWM_NC_ON//{ A_L_OFF;B_L_OFF;C_L_ON; }	



#if 1

#define SENSE_A 	CPT0MX = 0x20;
#define SENSE_B 	CPT0MX = 0x10;
#define SENSE_C		CPT0MX = 0x00;

#define SENSE				 (CPT0CN & 0x30)
#define SENSE_L              !(CPT0CN & 0x40)
#define SENSE_H              (CPT0CN & 0x40)

#define CMP_FALLING_INT		 CPT0CN & 0x10
#define CMP_RISING_INT		 CPT0CN & 0x20
 
		

#define ENABLE_SENSE_INT	 EIE1 |= 0x20
#define DISABLE_SENSE_INT	 EIE1 &= ~0x20

#define SENSE_FALLING_INT    CPT0MD = 0x10
#define SENSE_RISING_INT     CPT0MD = 0x20
#define SENSE_TOGGLE_INT     CPT0MD = 0x30

#define ENABLE_SENSE		 CPT0CN|=0x80;\
							 TimerDelayus(50);\
							 CPT0CN&=~0x30 	 	



#define EN_CP_INT			EIE1|=0x20
#define DIS_CP_INT			EIE1&=~0x20
#endif


#define EN_PCA0_INT		EIE1|=0x10
#define DIS_PCA0_INT  	EIE1&=(~0x10)

#define EN_PCA0			CR = 1
#define DIS_PCA0		CR = 0
			
#define EN_CCF_INT   	PCA0MD|=0x01
#define DIS_CCF_INT		PCA0MD&=0xFE

#define EN_CCF0_INT   	PCA0CPM0|=0x01
#define DIS_CCF0_INT	PCA0CPM0&=0xFE

#define EN_CCF1_INT   	PCA0CPM1|=0x01
#define DIS_CCF1_INT   	PCA0CPM1&=0xFE

#define START_ZC_CHECK			EN_CCF1_INT;//start zero cross checking
#define START_PWM_GENARATION 	EN_CCF_INT;\
								EN_PCA0	

void Commutator(void);
UINT8 GetCMDPWMValue();
void SetPWM(UINT16 pwm);
UINT8 MotorStartUp(UINT8 pm);
void MotorControl();
#endif